<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Export extends MY_Controller{ //CI_Controller{
    private $target_dir='';
    private $fields = array(
        'account_name'=>0,
        'fname'=>0,
        'lname'=>0,
        'password'=>0,
        'email'=>0,
        'store_name'=>0,
        'status'=>0, // If they upload zero status, I must delete
        // 'start_time'=>0, // Start time 07:00 in template but stored in DB as microtime
        // 'endtime'=>0,
    );
    
    public function __construct() {
        parent::__construct();
        //$this->acl_auth->restrict_access('admin');
        $this->target_dir = BASEPATH.'../import/';        
    }
    
    /**
     * Export  users
     * 
     */
    public function index(){
        $d=array();
        $d['title']='Export users';
        //$d['_configs']=$this->dbconfig->get_config();
        $views['_tabs'] = 'user/tabs';
        //$views['_userform'] = 'user/user_form';
        $views['_content'] = 'user/export';
        $this->template->load('user/admin', $views, $d);
    }
    public function alluser(){
        
        $this->load->model('user_model', 'um');
        $this->load->helper('file');
        $this->load->helper('download');
        $file = 'users.csv';
        
        $tmpfile = $this->target_dir.'../archived/temp-'.$file;
        
        $data = array('User Profile','Name','Username','E-mail', 'Last Activity');
        $users = $this->um->get_all_user(null); // get all users
        $fp = fopen($tmpfile, 'w');
        if ($fp){
            fputcsv($fp, $data);
            foreach ($users as $k => $user){
                $data = array(
                    $user->name==='admin'?'Admin':'Tiangge',
                     $user->fname.' '.$user->lname,
                     $user->account_name,
                     $user->email,
                    date('Y-m-d H:i:s', $user->last_activity)
                );

                fputcsv($fp, $data);
            }
            fclose($fp);
        }
        
        $this->setHearders($file, $tmpfile);
    }

    public function controlexport(){
        redirect('export/'.$this->input->post('action'));
    }
    
    public function inactive(){
        $this->target_dir = BASEPATH.'../import/';
        $this->load->model('user_model', 'um');
        $users = $this->um->get_users_status(0); // get all users
        $this->load->helper('file');
        $file = 'inactive-user.csv';
        
        $tmpfile = $this->target_dir.'../archived/temp-'.$file;
        $data = array('User Profile','Name','Username','E-mail');
        $fp = fopen($tmpfile, 'w');
        if ($fp){
            fputcsv($fp, $data);

            foreach ($users as $k => $user){
                $data = array(
                    $user->name==='admin'?'Admin':'Tiangge',
                     $user->fname.' '.$user->lname,
                     $user->account_name,
                     $user->email,
                );
                fputcsv($fp, $data);
                //write_file($tmpfile, json_encode($data)."\n", 'a+');            
            }
            fclose($fp);
        }

        $this->setHearders($file, $tmpfile);
    }
    public function active(){
        $this->target_dir = BASEPATH.'../import/';
        $this->load->model('user_model', 'um');
        $users = $this->um->get_users_status(1); // get all users
        $this->load->helper('file');
        $file = 'inactive-user.csv';
        
        $tmpfile = $this->target_dir.'../archived/temp-'.$file;
        $data = array('User Profile','Name','Username','E-mail');
        $fp = fopen($tmpfile, 'w');
        if ($fp){
            fputcsv($fp, $data);                
            foreach ($users as $k => $user){
                $data = array(
                    $user->name==='admin'?'Admin':'Tiangge',
                     $user->fname.' '.$user->lname,
                     $user->account_name,
                     $user->email,
                );
                fputcsv($fp, $data);
                //write_file($tmpfile, json_encode($data)."\n", 'a+');            
            }
            fclose($fp);
        }

        $this->setHearders($file, $tmpfile);
    }

    public function dormant(){
        $this->target_dir = BASEPATH.'../import/';
        $this->load->model('user_model', 'um');
        $users = $this->um->get_dormant_user(); // get all users
        $this->load->helper('file');
        $file = 'dormant-user.csv';
        
        $tmpfile = $this->target_dir.'../archived/temp-'.$file;
        $data = array('User Profile','Name','Username','E-mail');
        $fp = fopen($tmpfile, 'w');
        if ($fp){
            fputcsv($fp, $data);                

            foreach ($users as $k => $user){
                $data = array(
                    $user->name==='admin'?'Admin':'Tiangge',
                     $user->fname.' '.$user->lname,
                     $user->account_name,
                     $user->email,
                     date('Y-m-d H:i:s', $user->last_activity)
                );
            
                fputcsv($fp, $data);
                //write_file($tmpfile, json_encode($data)."\n", 'a+');            
            }
            fclose($fp);
        }
        $this->setHearders($file, $tmpfile);

    }
    
    private function setHearders($file, $fullpath){
        header("Content-Disposition: attachment; filename=\"$file\"");
        header("Content-Type: text/csv");
        header('Expires: 0');
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//            header("Content-Transfer-Encoding: binary");
        header("Pragma: no-cache");
        header("Content-Length: ".  filesize($fullpath));
        header("Connection: close");
        readfile($fullpath);

    }
} //   
